using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.UI.HtmlControls;

namespace MessageBoardControl
{
	/// <summary>
	/// Summary description for AddThread.
	/// </summary>
	[DefaultProperty("Text"), 
		ToolboxData("<{0}:AddThread runat=server></{0}:AddThread>")]
	public class AddPost : System.Web.UI.WebControls.WebControl, INamingContainer
	{
		#region Properties
		private string text;
	
		[Bindable(true), 
			Category("Appearance"), 
			DefaultValue("")] 
		public string Text 
		{
			get
			{
				return text;
			}

			set
			{
				text = value;
			}
		}

		private string urlAfter;
		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public string URLAfter 
		{
			get
			{
				return urlAfter;
			}

			set
			{
				urlAfter = value;
			}
		}

		private MessageBoard.Thread thread;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public MessageBoard.Thread Thread 
		{
			get
			{
				return thread;
			}

			set
			{
				thread = value;
			}
		}

		private MessageBoard.User currentUser;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public MessageBoard.User CurrentUser 
		{
			get
			{
				return currentUser;
			}

			set
			{
				currentUser = value;
			}
		}

		private MessageBoard.Language language;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public MessageBoard.Language Language 
		{
			get
			{
				return language;
			}

			set
			{
				language = value;
			}
		}
		#endregion
		
		#region Translation
		private MessageBoard.CustomControl customControl = MessageBoard.CustomControl.Get("AddPost");

		private string Translate(string word)
		{
			return MessageBoard.Translation.Get(customControl, word, Language).Equal;
		}
		#endregion

		/// <summary> 
		/// Render this control to the output parameter specified.
		/// </summary>
		/// <param name="output"> The HTML writer to write out to </param>
		//protected override void Render(HtmlTextWriter output)
		//{
		//	output.Write(Text);
		//}

		protected override void CreateChildControls() 
		{
			// Check the required permissions
			if(MessageBoard.Role.Check(currentUser, Thread, MessageBoard.Role.Permission.CanAddPosts) !=  MessageBoard.Role.Allow.Yes)
			{
				return;
			}

			// Check if the thread is locked
			if(thread.Locked)
				return;

			CurrentUser.MakeRequest();

			TextBox txtText = new TextBox();
			Button cmdAdd = new Button();
			

			// Define the body text box.
			txtText.Text="";
			txtText.TextMode = TextBoxMode.MultiLine;
			txtText.Height = 200;
			txtText.Width = 500;
			txtText.ID = "txtText";

			cmdAdd.Text = Translate("Add");



			// Put the contols in a table.
			HtmlTable table = new HtmlTable();
			table.Attributes.Add("width", "100%");
			HtmlTableRow tr0 = new HtmlTableRow();
			table.Controls.Add(tr0);
			HtmlTableCell td0 = new HtmlTableCell();
			tr0.Controls.Add(td0);
			td0.Attributes.Add("class", "Forum_Title1");
			td0.InnerHtml = Translate("Add reply");

			HtmlTableRow tr01 = new HtmlTableRow();
			table.Controls.Add(tr01);
			HtmlTableCell td01 = new HtmlTableCell();
			tr01.Controls.Add(td01);
			td01.Attributes.Add("class", "Forum_Title4");

			// Table 2
			HtmlTable table2 = new HtmlTable();
			table2.Attributes.Add("width", "100%");

			// Text body
			HtmlTableRow tr2 = new HtmlTableRow();
			table2.Controls.Add(tr2);
			HtmlTableCell td21 = new HtmlTableCell();
			HtmlTableCell td22 = new HtmlTableCell();
			td21.InnerHtml = Translate("Message")+":";
			td21.Attributes.Add("class", "Forum_Text");
			td21.Attributes.Add("vAlign", "top");
			td22.Controls.Add(txtText);
			tr2.Controls.Add(td21);
			tr2.Controls.Add(td22);


			// Button
			HtmlTableRow tr3 = new HtmlTableRow();
			table2.Controls.Add(tr3);
			HtmlTableCell td31 = new HtmlTableCell();
			HtmlTableCell td32 = new HtmlTableCell();
			td31.InnerHtml = "";
			td32.Controls.Add(cmdAdd);
			tr3.Controls.Add(td31);
			tr3.Controls.Add(td32);

			td01.Controls.Add(table2);

			HtmlTable tableOut = new HtmlTable();
			HtmlTableRow trOut = new HtmlTableRow();
			HtmlTableCell tdOut = new HtmlTableCell();
			tableOut.BgColor = "#000000";
			tableOut.Attributes.Add("width", "100%");
			tableOut.CellPadding = 0;
			tableOut.CellSpacing = 0;
			table.CellSpacing = 1;
			tdOut.Controls.Add(table);
			trOut.Controls.Add(tdOut);
			tableOut.Controls.Add(trOut);
			Controls.Add(tableOut);

			cmdAdd.Click += new EventHandler(cmdAdd_Click);
                  
		}

		public void cmdAdd_Click(Object sender, EventArgs e)
		{
			TextBox txtText = ((TextBox)FindControl("txtText"));
			if(txtText.Text!="")
			{
				MessageBoard.Post post = new MessageBoard.Post();
				post.Text = txtText.Text;
				post.Thread = thread;
				post.User = CurrentUser;
				post.IsFirstPostInThread = false;
				post.Insert();

				thread.TotalPosts++;
				thread.Update();

				txtText.Text="";
				this.EnsureChildControls();
				this.Page.Response.Redirect(urlAfter);
			}
		}
	}
}
